package com.example.demo.controller;

import com.example.demo.entity.Order;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    public int addOrder(@RequestBody Order order) {
        return orderService.addOrder(order);
    }

    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable int id) {
        return orderService.getOrderById(id);
    }

    @PutMapping("/update")
    public int updateOrder(@RequestBody Order order) {
        return orderService.updateOrder(order);
    }

    @DeleteMapping("/delete/{id}")
    public int deleteOrder(@PathVariable int id) {
        return orderService.deleteOrder(id);
    }

    @GetMapping("/list")
    public List<Order> getAllOrders() {
        return orderService.getAllOrders();
    }

    // 根据订单ID查询订单和用户信息
    @GetMapping("/details/{id}")
    public Order getOrderWithUserDetails(@PathVariable int id) {
        return orderService.getOrderWithUserDetails(id);
    }
    // 根据用户ID查询订单
    @GetMapping("/user/{userId}")
    public List<Order> getOrdersByUserId(@PathVariable int userId) {
        return orderService.getOrdersByUserId(userId);
    }
    //关键字查询
    @GetMapping("/search/user")
    public List<Order> getOrdersByUserName(@RequestParam String keyword) {
        return orderService.getOrdersByUserName(keyword);
    }

}